package org.xxpay.apibase.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* @Description: 时间工具类 （对外提供通用函数， 尽量保证代码简洁， 可扩展 ）
* @author xxpay
*/
public class DateKit {

    public static final String FORMAT_DEFAULT = "yyyy-MM-dd HH:mm:ss";
    public static final String FORMAT_DATE = "yyyy-MM-dd";
    public static final String FORMAT_APPEND = "yyyyMMddHHmmss";
    public static final String FORMAT_FULL = "yyyyMMddHHmmssSSS";
    public static final String FORMAT_STR = "yyyy年MM月dd日 HH点mm分";

    public static long getNowTimestamp() {
        return new Date().getTime();
    }

    public static String getNowTimeStr(){
        return getNowTime(FORMAT_DEFAULT);
    }

    public static String getNowTime(String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

    public static Date str2Date(String dateStr){
        return str2Date(dateStr, FORMAT_DEFAULT);
    }

    public static Date str2Date(String dateStr, String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
        }
        return null;
    }

    public static String date2Str(String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date());
    }

    public static String date2Str(Date date, String format){
        if(date == null) return null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);

    }

}
